﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;
using System.Collections;//Please add references
namespace SCS.DAL
{
    /// <summary>
    /// 数据访问类:SCS_Roler
    /// </summary>
    public partial class SCS_Roler
    {
        public SCS_Roler()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Roler_ID", "SCS_Roler");
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Roler_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SCS_Roler");
            strSql.Append(" where Roler_ID=" + Roler_ID + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SCS.Model.SCS_Roler model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.Roler_Name != null)
            {
                strSql1.Append("Roler_Name,");
                strSql2.Append("'" + model.Roler_Name + "',");
            }
            if (model.Roler_Description != null)
            {
                strSql1.Append("Roler_Description,");
                strSql2.Append("'" + model.Roler_Description + "',");
            }
            if (model.Roler_IsEnable != null)
            {
                strSql1.Append("Roler_IsEnable,");
                strSql2.Append("" + model.Roler_IsEnable + ",");
            }
            strSql.Append("insert into SCS_Roler(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SCS.Model.SCS_Roler model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SCS_Roler set ");
            if (model.Roler_Name != null)
            {
                strSql.Append("Roler_Name='" + model.Roler_Name + "',");
            }
            else
            {
                strSql.Append("Roler_Name= null ,");
            }
            if (model.Roler_Description != null)
            {
                strSql.Append("Roler_Description='" + model.Roler_Description + "',");
            }
            else
            {
                strSql.Append("Roler_Description= null ,");
            }
            if (model.Roler_IsEnable != null)
            {
                strSql.Append("Roler_IsEnable=" + model.Roler_IsEnable + ",");
            }
            else
            {
                strSql.Append("Roler_IsEnable= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where Roler_ID=" + model.Roler_ID + "");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Roler_ID, SCS.Model.SCS_Operating_Log tOperatorLog, List<SCS.Model.SCS_Roler> tListRoler)
        {
            List<string> strSqlList = new List<string>();

            string strDeleteSQL ="delete from SCS_Roler where Roler_ID=" + Roler_ID;

            strSqlList.Add(strDeleteSQL);
            strSqlList.AddRange(GenerateRolerLog(tListRoler,tOperatorLog));
            strSqlList.Add(SCS.DAL.SCS_Operating_Log.GenerateOperatorLogSQL(tOperatorLog));

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(strSqlList);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Roler_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SCS_Roler ");
            strSql.Append(" where Roler_ID in (" + Roler_IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SCS.Model.SCS_Roler GetModel(int Roler_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" Roler_ID,Roler_Name,Roler_Description,Roler_IsEnable ");
            strSql.Append(" from SCS_Roler ");
            strSql.Append(" where Roler_ID=" + Roler_ID + "");
            SCS.Model.SCS_Roler model = new SCS.Model.SCS_Roler();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SCS.Model.SCS_Roler DataRowToModel(DataRow row)
        {
            SCS.Model.SCS_Roler model = new SCS.Model.SCS_Roler();
            if (row != null)
            {
                if (row["Roler_ID"] != null && row["Roler_ID"].ToString() != "")
                {
                    model.Roler_ID = int.Parse(row["Roler_ID"].ToString());
                }
                if (row["Roler_Name"] != null)
                {
                    model.Roler_Name = row["Roler_Name"].ToString();
                }
                if (row["Roler_Description"] != null)
                {
                    model.Roler_Description = row["Roler_Description"].ToString();
                }
                if (row["Roler_IsEnable"] != null && row["Roler_IsEnable"].ToString() != "")
                {
                    model.Roler_IsEnable = int.Parse(row["Roler_IsEnable"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Roler_ID,Roler_Name,Roler_Description,Roler_IsEnable ");
            strSql.Append(" FROM SCS_Roler ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Roler_ID,Roler_Name,Roler_Description,Roler_IsEnable ");
            strSql.Append(" FROM SCS_Roler ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM SCS_Roler ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Roler_ID desc");
            }
            strSql.Append(")AS Row, T.*  from SCS_Roler T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  Method
        #region  MethodEx
        public int UpdateRoler(List<SCS.Model.SCS_Roler> tListRolerUpdate, List<SCS.Model.SCS_Roler> tListRolerLog, SCS.Model.SCS_Operating_Log tOperatorLog)
        {
            List<string> sqlList = new List<string>();
            try
            {
                sqlList.Add(SCS.DAL.SCS_Operating_Log.GenerateOperatorLogSQL(tOperatorLog));

                sqlList.AddRange(GenerateRoler(tListRolerUpdate));

                sqlList.AddRange(GenerateRolerLog(tListRolerLog,tOperatorLog));

                DbHelperSQL.ExecuteSqlTran(sqlList);
            }
            catch (Exception)
            {
                return 0;
            }

            return 1;
        }

        public static List<string> GenerateRoler(List<SCS.Model.SCS_Roler> tListRoler)
        {
            List<string> oListSQL = new List<string>();

            if (tListRoler != null && tListRoler.Count > 0)
            {
                foreach (SCS.Model.SCS_Roler item in tListRoler)
                {
                    if (item != null)
                    {
                        string strRolerSQL = "UPDATE SCS_Roler SET ROLER_NAME = '" + item.Roler_Name + "',ROLER_DESCRIPTION = '" + item.Roler_Description + "',ROLER_ISENABLE = '" + item.Roler_IsEnable + "' WHERE ROLER_ID = '" + item.Roler_ID + "'";

                        oListSQL.Add(strRolerSQL);
                    }
                }
            }
            return oListSQL;
        }

        public static List<string> GenerateRolerLog(List<SCS.Model.SCS_Roler> tListRolerLog, SCS.Model.SCS_Operating_Log tOperatorLog)
        {
            List<string> oListLogSQL = new List<string>();

            if (tListRolerLog != null && tListRolerLog.Count > 0 && tOperatorLog != null)
            {
                foreach (SCS.Model.SCS_Roler item in tListRolerLog)
                {
                    if (item != null)
                    {
                        string strRolerLogSQL = "INSERT INTO SCS_Roler_Audit(ROLER_NAME,ROLER_DESCRIPTION,ROLER_ISENABLE,ROLER_ID,UPDATED_DATE,UPDATED_BY,LOG_ID) " +
                        "VALUES( '" + item.Roler_Name + "', '" + item.Roler_Description + "','" + item.Roler_IsEnable + "', '" + item.Roler_ID + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + tOperatorLog.Log_User_ID + "',@@identity)";

                        oListLogSQL.Add(strRolerLogSQL);
                    }
                }
            }
            return oListLogSQL;
        }

        #endregion  MethodEx
    }
}

